import http from "@/api/http";

export interface Api {
  query<T>(id: String | Number): Promise<T>;

  list<T>(params: any): Promise<T>;

  add(params: any): Promise<any>;

  edit(params: any): Promise<any>;

  remove(id: String | Number): Promise<any>;

  removes(ids: Array<String> | Array<Number>): Promise<any>;
}

export class BaseApi implements Api {
  readonly url: string = "";
  constructor(url: string) {
    this.url = url;
  }

  query = <T>(id: String | Number): Promise<T> => {
    const params = { id };
    return http.get(this.url + "/query", params);
  };
  list = <T>(params: any): Promise<T> => {
    return http.get(this.url + "/list", params);
  };

  add = (params: any): Promise<any> => {
    return http.post(this.url + "/add", params);
  };

  edit = (params: any): Promise<any> => {
    return http.put(this.url + "/edit", params);
  };

  remove = (id: String | Number): Promise<any> => {
    const params = { id };
    return http.delete(this.url + "/remove", params);
  };

  removes = (ids: Array<String> | Array<Number>): Promise<any> => {
    const params = { ids };
    return http.delete(this.url + "/removes", params);
  };
}
